package com.example.demo.controller;

import com.example.demo.api.TestFeignClient;
import com.example.demo.entity.MyProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RefreshScope
public class TestController {
    private Logger log = LoggerFactory.getLogger(TestController.class);
//    @Autowired
//    private TestFeignClient testFeignClient;

//    @Value("${app:demo}")
//    private String app;
//    @Value("${version:1}")
//    private String version;
//    @GetMapping("test")
//    public String test() {
//        log.info("调用test方法");
//        return "test1: " + app + version;
//    }

//    @GetMapping("call")
//    public String callService2() {
//        log.info("调用call方法。。。");
//        return testFeignClient.test();
//    }
    @Autowired
    private MyProperties myProperties;

    @Value("${spring.application.name:blank}")

    private String appName;

    @Value("${app.name:blank}")

    private String name;
    @GetMapping("key")
    public String testKey() {
        return "查看：" + name;
        //return "查看：" + myProperties.getName();
    }
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("config")
    @HystrixCommand(fallbackMethod = "configFallback")
    public String getFromConfig(){
        return "展示配置文件name的值：" + appName;

    }

    public String configFallback(){
        return "错误，熔断";
    }

    @GetMapping("call")
    @HystrixCommand(fallbackMethod = "myFallback")
    public String call() {
        return restTemplate.getForObject("http://localhost:7009/test", String.class);
    }

    public String myFallback(){
        return "错误";
    }



}
